package com.shievy.anweb.webview.api

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.auto.service.AutoService
import com.shievy.anweb.common.service.WebViewService
import com.shievy.anweb.webview.web.activity.WebViewFragment
import com.shievy.anweb.webview.web.activity.WebViewFragmentActivity
import com.shievy.anweb.webview.uitl.Constants

@AutoService(WebViewService::class)
class WebViewServiceImpl : WebViewService {
    override fun openFragmentPage(cxt: Context, bundle: Bundle) {
        cxt.startActivity(Intent(cxt, WebViewFragmentActivity::class.java).apply {
            putExtras(bundle)
        })
    }


    override fun openFragmentPage(cxt: Context, url: String, title: String, isShowBar: Boolean) =
        openFragmentPage(cxt, Bundle().apply {
            putString(Constants.WEB_URL, url)
            putString(Constants.WEB_BAR_TITLE, title)
            putBoolean(Constants.WEB_BAR_IS_SHOW, isShowBar)
        })

    override fun getWebViewFragment(url: String, bundle: Bundle): Fragment =
        WebViewFragment.createWebViewFragment(url, bundle)

    override fun loadAssetsFile(cxt: Context, filePath: String, title: String, isShowBar: Boolean) {
        openFragmentPage(
            cxt,
            if (filePath.contains(Constants.ANDROID_ASSET_URI)) filePath else "${Constants.ANDROID_ASSET_URI}$filePath",
            title,
            isShowBar
        )
    }
}